# dejagnu.mk - dejagnu support.
# Much of this is taken from Automake.

# The default tool to use.  This can be a list of tool names as well.
# The test suite for each tool can be run in parallel; note that they
# are all run in the same directory.  FIXME: allow separate
# directories; gcc does this.
DEJATOOL = $(PACKAGE_NAME)

RUNTEST = runtest

RUNTESTDEFAULTFLAGS = --srcdir $(abs_srcdir)/$(DEJAGNU_TEST_DIR)
RUNTESTFLAGS =

ifeq ($(DEJATOOL),)
$(error dejagnu support activated but DEJATOOL is empty)
endif

# Run all the tests.
quagmire/check-dejagnu: $(addprefix quagmire/check-deja-,$(DEJATOOL))
	@true

.PHONY: quagmire/check-dejagnu $(addprefix quagmire/check-deja-,$(DEJATOOL))
check: quagmire/check-dejagnu

# quagmire/check-one-deja TOOL
# Expand to check a single tool.
# FIXME: Using a pattern rule here did not work for me.  GNU make bug?
# My misunderstanding?
define quagmire/check-one-deja

# Unlike automake we simply run dejagnu.  If it does not exist, we
# fail.
quagmire/check-deja-$(1): | $(DEJAGNU_TEST_DIR)/site.exp
	cd $$(DEJAGNU_TEST_DIR) && $$(RUNTEST) $$(RUNTESTDEFAULTFLAGS) $$(RUNTESTFLAGS) --tool $(1)

endef

$(foreach _tool,$(DEJATOOL),$(eval $(call quagmire/check-one-deja,$(_tool))))


# Make the test directory.
$(DEJAGNU_TEST_DIR):
	@mkdir -p $(DEJAGNU_TEST_DIR)

# Make the site.exp file.  FIXME: the pre-depend on the test dir does
# not seem to work, so we explicitly make the directory here.
$(DEJAGNU_TEST_DIR)/site.exp: Makefile | $(DEJAGNU_TEST_DIR) .quagmire
	@$(call quagmire/echo,'Making a new $(DEJAGNU_TEST_DIR)/site.exp file...')
	@mkdir -p $(DEJAGNU_TEST_DIR)
	@echo '## these variables are automatically generated by make ##' >.quagmire/site.tmp
	@echo '# Do not edit here.  If you wish to override these values' >>.quagmire/site.tmp
	@echo '# edit the last section' >>.quagmire/site.tmp
	@echo 'set srcdir $(srcdir)' >>.quagmire/site.tmp
	@echo "set objdir `pwd`" >>.quagmire/site.tmp
	@echo 'set build_alias "$(build_alias)"' >>.quagmire/site.tmp
	@echo 'set build_triplet $(build_triplet)' >>.quagmire/site.tmp
	@echo 'set host_alias "$(host_alias)"' >>.quagmire/site.tmp
	@echo 'set host_triplet $(host_triplet)' >>.quagmire/site.tmp
	@echo 'set target_alias "$(target_alias)"' >>.quagmire/site.tmp
	@echo 'set target_triplet $(target_triplet)' >>.quagmire/site.tmp
	@echo '## All variables above are generated by configure. Do Not Edit ##' >>.quagmire/site.tmp
	@test ! -f site.exp || \
	  sed '1,/^## All variables above are.*##/ d' site.exp >> .quagmire/site.tmp
	@-rm -f $(DEJAGNU_TEST_DIR)/site.bak
	@test ! -f $(DEJAGNU_TEST_DIR)/site.exp || mv $(DEJAGNU_TEST_DIR)/site.exp $(DEJAGNU_TEST_DIR)/site.bak
	@mv .quagmire/site.tmp $(DEJAGNU_TEST_DIR)/site.exp

distclean/dejagnu:
	rm -f $(DEJAGNU_TEST_DIR)/site.exp $(DEJAGNU_TEST_DIR)site.bak
	rm -f $(addprefix $(DEJAGNU_TEST_DIR)/,$(addsuffix .log,$(DEJATOOL)))
	rm -f $(addprefix $(DEJAGNU_TEST_DIR)/,$(addsuffix .sum,$(DEJATOOL)))
.PHONY: distclean/dejagnu
distclean: distclean/dejagnu
